package abg.core.service.sys.impl;

import abg.core.common.cache.Cache;
import abg.core.common.util.*;
import abg.core.common.util.valid.NotBlank;
import abg.core.common.util.valid.base.Valid;
import abg.core.domain.db.SysFile;
import abg.core.domain.search.Psh;
import abg.core.domain.sys.FileBType;
import abg.core.domain.sys.MSG;
import abg.core.domain.sys.ServiceException;
import abg.core.mapper.SysFileMapper;
import abg.core.service.sys.SysFileService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

import static abg.core.domain.db.table.SysFileTableDef.SYS_FILE;

/**
 * 文件操作
 */
@Slf4j
@Service
public class SysFileServiceImpl implements SysFileService {

    @Resource
    private Cache<String> cache;

    @Resource
    private MinioKit minioKit;

    @Resource
    private SysFileMapper fileMapper;

    @Valid
    @Override
    public SysFile uploadImgFile(@NotBlank MultipartFile file, String uploadUserId, @NotBlank FileBType bType) {
        InputStream inputStreamMd5;
        InputStream inputStreamUpload;
        try {
            inputStreamMd5 = file.getInputStream();
            inputStreamUpload = file.getInputStream();
        } catch (IOException e) {
            throw new ServiceException(MSG.FILE_UPLOAD_ER, e.getMessage());
        }
        String originName = file.getOriginalFilename();
        if (StringKit.isBlank(originName))
            throw new ServiceException(MSG.FILE_UPLOAD_ER, "文件名不能为空");

        //上传至第三方云服务或服务器
        String fileType = originName.substring(originName.lastIndexOf(".") + 1);
        if (!StringKit.has(fileType, "png", "jpg"))
            throw new ServiceException(MSG.FILE_UPLOAD_ER, "文件类型不满足，请上传 png/jpg");

        String id = IdKit.getId();
        String reName = id + "." + fileType;

        //查一下库里有没有MD5，有就不用上传了,防止多次上传一个文件，浪费资源
        //硬存
        String md5 = FileKit.getMD5(inputStreamMd5);
        String URI = minioKit.uploadGetURI(inputStreamUpload, reName, file.getContentType());

        //
        //        String md5 = FileKit.getMD5(inputStreamMd5);
        //        SysFile sysFile = fileMapper.getOneUriByMd5(md5);
        //        String URI;
        //        if (sysFile != null) {
        //            URI = sysFile.getUri();
        //        } else {
        //            URI = minioFileKit.uploadGetURI(inputStreamUpload, reName, "image/png");
        //        }

        //保存数据信息至数据库
        SysFile sysFile = new SysFile();
        sysFile.setId(id);
        sysFile.setName(reName);
        sysFile.setUri(URI);
        sysFile.setOriginName(originName);
        sysFile.setFileType(fileType);
        sysFile.setBType(bType);
        sysFile.setUploadUserId(uploadUserId);
        sysFile.setSize(file.getSize());
        sysFile.setMd5(md5);
        fileMapper.insertSelective(sysFile);
        return sysFile;
    }

    @Valid
    @Override
    public SysFile uploadImgFile(@NotBlank String base64, String uploadUserId, FileBType bType) {
        return uploadImgFile(MultipartFileKit.base64Convert(base64), uploadUserId, bType);
    }

    @Override
    public Page<SysFile> page(SysFile sysFile, Psh psh) {
        QueryWrapper wrapper = QueryWrapper.create()
                .select(SYS_FILE.ALL_COLUMNS)
                .from(SysFile.class)
                .and(SYS_FILE.ORIGIN_NAME.like(sysFile.getOriginName()))
                .orderBy(SYS_FILE.CT.desc());
        return fileMapper.paginate(psh, wrapper);
    }
}
